import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/page/home/model/cer_category_entity.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';

/// 选中资质的回调
///  levelIndex 层级
///  itemIndex item索引
///  cityModel 选中的数据
typedef SelectDropMenuCerCallback(
    int levelIndex, int itemIndex, CerCategoryEntity categoryEntity);

class DropMenuCertificateWidget extends StatefulWidget {
  /// 资质数据
  final List<CerCategoryEntity> categoryDatas;

  /// 选择之后的回调
  final SelectDropMenuCerCallback selectCerCallback;

  const DropMenuCertificateWidget(
      {Key key, this.categoryDatas, this.selectCerCallback})
      : super(key: key);

  @override
  _DropMenuCertificateWidgetState createState() =>
      _DropMenuCertificateWidgetState();
}

// ignore: must_be_immutable
class _DropMenuCertificateWidgetState extends State<DropMenuCertificateWidget> {
  int _selectTempFirstLevelIndex = 0;
  int _selectFirstLevelIndex = 0;
  int _selectSecondLevelIndex = -1;

  /// 行高
  final double cellHeight = ScreenAdapter.height(90);

  @override
  Widget build(BuildContext context) {
    return _buildAddressWidget();
  }

  _buildAddressWidget() {
    return IntrinsicHeight(
      child: Row(
        children: <Widget>[
          Container(
            width: ScreenAdapter.width(300),
            child: ListView(
              children: widget.categoryDatas.map((model) {
                int index = widget.categoryDatas.indexOf(model);
                return InkWell(
                  onTap: () {
                    _selectTempFirstLevelIndex = index;
                    widget.selectCerCallback(0, index, model);
                  },
                  child: Container(
                      height: cellHeight,
                      color: _selectTempFirstLevelIndex == index
                          ? ThemeColors.colorType13
                          : ThemeColors.colorWhite,
                      alignment: Alignment.center,
                      child: _selectTempFirstLevelIndex == index
                          ? Text(
                              model.title,
                              style: TextStyle(
                                  color: ThemeColors.colorType1,
                                  fontSize: ScreenAdapter.sp(30)),
                            )
                          : Text(model.title)),
                );
              }).toList(),
            ),
          ),
          Expanded(
            child: Container(
              color: ThemeColors.colorType13,
              child:
                  // _selectTempFirstLevelIndex == 0 ? Container() :
                  ListView(
                children: widget
                    .categoryDatas[_selectTempFirstLevelIndex].children
                    .map((model) {
                  int index = widget
                      .categoryDatas[_selectTempFirstLevelIndex].children
                      .indexOf(model);
                  return InkWell(
                      onTap: () {
                        _selectSecondLevelIndex = index;
                        _selectFirstLevelIndex = _selectTempFirstLevelIndex;
                        widget.selectCerCallback(1, index, model);
                      },
                      child: Container(
                        height: cellHeight,
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.symmetric(
                            horizontal: ScreenAdapter.width(20)),
                        child: _selectFirstLevelIndex ==
                                    _selectTempFirstLevelIndex &&
                                _selectSecondLevelIndex == index
                            ? Text(
                                model.title,
                                softWrap: true,
                                overflow: TextOverflow.fade,
                                style: TextStyle(
                                    color: ThemeColors.colorType1,
                                    fontSize: ScreenAdapter.sp(30)),
                              )
                            : Text(model.title),
                      ));
                }).toList(),
              ),
            ),
          )
        ],
      ),
    );
  }
}
